<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="118px">
      <el-form-item :label="$t('ota.upgradeTask.planName')" prop="planName">
        <el-input v-model="queryParams.planName" :placeholder="$t('ota.upgradeTask.placeholder.planName')" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>

      <el-form-item :label="$t('ota.upgradeTask.userType')" prop="userType">
        <el-select v-model="queryParams.userType" clearable size="small" style="width: 240px">
          <el-option v-for="dict in dict.type.user_type"
                     :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item :label="$t('device.center.import.deviceTypeId')" prop="deviceTypeId">
        <el-select v-model="queryParams.deviceTypeId" clearable size="small" style="width: 240px" @change="deviceTypeChange">
          <el-option v-for="dict in deviceTypeOptions"
                     :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('device.center.import.deviceModelId')" prop="deviceModelId">
        <el-select v-model="queryParams.deviceModelId" clearable size="small" style="width: 240px">
          <el-option v-for="dict in deviceModelOptions"
                     :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('common.button.query')}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t('common.button.reset')}}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['system:firmwareUpdatePlan:export']">{{$t('common.button.export')}}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="firmwareUpdatePlanList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :label="$t('ota.upgradeTask.planName')" align="center" prop="planName" :show-overflow-tooltip="true" width="120" />
      <el-table-column :label="$t('ota.upgradeTask.userType')" align="center" prop="userType" :show-overflow-tooltip="true" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.user_type" :value="scope.row.userType" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('ota.version.deviceTypeName')" align="center" prop="deviceTypeName" :show-overflow-tooltip="true" width="120" />
      <el-table-column :label="$t('ota.version.deviceModelName')" align="center" prop="deviceModelName" :show-overflow-tooltip="true" width="120" />
      <el-table-column :label="$t('ota.version.versionName')" align="center" prop="versionNo" :show-overflow-tooltip="true" width="120" />
      <el-table-column :label="$t('ota.upgradeTask.upgradeTime')" align="center" prop="upgradeTime" :show-overflow-tooltip="true" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.upgradeTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('ota.upgradeTask.overtimeTime')" align="center" prop="overtimeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.overtimeTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column :label="$t('ota.upgradeTask.upgradeTotal')" align="center" prop="upgradeTotal" width="180" />
      <el-table-column :label="$t('ota.upgradeTask.upgradeWaitTotal')" align="center" prop="upgradeWaitTotal" width="180" />
      <el-table-column :label="$t('ota.upgradeTask.upgradingTotal')" align="center" prop="upgradingTotal" width="180" />
      <el-table-column :label="$t('ota.upgradeTask.upgradeSuccessTotal')" align="center" prop="upgradeSuccessTotal" width="180" />
      <el-table-column :label="$t('ota.upgradeTask.upgradeFailTotal')" align="center" prop="upgradeFailTotal" width="180" />
      <el-table-column :label="$t('ota.upgradeTask.upgradeTerminateTotal')" align="center" prop="upgradeTerminateTotal" width="180" />
      <el-table-column :label="$t('ota.upgradeTask.upgradeTimeoutTotal')" align="center" prop="upgradeTimeoutTotal" width="180" />

      <el-table-column :label="$t('ota.upgradeTask.isClose')" align="center" prop="isClose" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isClose" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('ota.upgradeTask.comment')" align="center" prop="comment" width="220" />

      <el-table-column :label="$t('common.table.operate')" fixed="right" align="center" class-name="small-padding fixed-width" width="220">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="viewDetails(scope.row)"
            v-hasPermi="['ota:upgradeTask:detail']"
          >{{$t('ota.upgradeTask.viewDetails')}}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-close"
            :disabled="scope.row.isClose !== 'N'"
            @click="cancelUpgrade(scope.row.planId)"
            v-hasPermi="['ota:upgradeTask:cancel']"
          >{{$t('ota.upgradeTask.cancelUpgrade')}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <Details ref="Details"></Details>

  </div>
</template>

<script>
import { listFirmwareUpdatePlan, cancelUpgrade } from "@/api/ota/upgrade";
import { typeTree } from "@/api/device/type";
import { modelTree } from "@/api/device/model";
import Details from "./modules/Details";

export default {
  name: "FirmwareUpdatePlan",
  components: {Details},
  dicts: ['sys_yes_no', 'user_type'],
  data() {
    return {
      showDeviceDialog: false,
      deviceTypeOptions: [],
      deviceModelOptions: [],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备升级任务计划表格数据
      firmwareUpdatePlanList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planName: undefined,
        comment: undefined,
        deviceTypeId: undefined,
        deviceModelId: undefined,
        userType: undefined,
        versionId: undefined,
        upgradeTime: undefined,
        overtimeTime: undefined,
        isClose: undefined,
      },
    };
  },
  created() {
    this.getList();
    this.getTypeTree();
  },
  methods: {
    viewDetails(row) {
      this.$refs['Details'].init(row.planId);
    },
    cancelUpgrade(planId) {
      const message = this.$t('ota.upgradeTask.cancelUpgradeConfirm');
      const title = this.$t('common.prompt.tip');
      this.$confirm(message, title, {
        confirmButtonText: this.$t('common.button.confirm'),
        cancelButtonText: this.$t('common.button.cancel'),
        type: "warning",
      }).then(function () {
        return cancelUpgrade(planId)
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('ota.upgradeTask.cancelUpgradeSuccess'));
      })
    },
    deviceTypeChange(typeId) {
      if (typeId) {
        this.getModelTree(typeId)
      } else {
        this.queryParams.deviceModelId = undefined
        this.deviceModelOptions = []
      }
    },
    getTypeTree() {
      typeTree().then(response => {
        this.deviceTypeOptions = response.data;
      });
    },
    getModelTree(typeId) {
      modelTree(typeId).then(response => {
        this.deviceModelOptions = response.data;
      });
    },
    /** 查询设备升级任务计划列表 */
    getList() {
      this.loading = true;
      listFirmwareUpdatePlan(this.queryParams).then(response => {
        this.firmwareUpdatePlanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('device/upgrade/export', {
        ...this.queryParams
      }, `firmwareUpdatePlan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
